# defivelo-intranet -- Outil métier pour la gestion du Défi Vélo
# Copyright (C) 2015 Didier Raboud <me+defivelo@odyx.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
import glob
import os


def import_env_vars(directory):
    """
    List the files present in the given directory and for each of them create
    an environment variable named after the file, and which value is the
    contents of the file.
    """
    env_vars = glob.glob(os.path.join(directory, "*"))

    for env_var in env_vars:
        if os.path.isfile(env_var):
            with open(env_var, "r") as env_var_file:
                os.environ.setdefault(
                    env_var.split(os.sep)[-1], env_var_file.read().strip()
                )


def get_project_root_path():
    """
    Return the absolute path to the root of the project.
    """
    return os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir))
